package com.ruoyi.web.controller.app;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.BusiException;
import com.ruoyi.common.exception.StatusException;
import com.ruoyi.system.service.ISysUserService;

import javax.annotation.Resource;

public class AppController extends BaseController {

    @Resource
    private ISysUserService userService;

    /**
     * 获取登录用户id
     */
    @Override
    public Long getUserId() {
        Long userId = getLoginUser().getUserId();
        SysUser sysUser = userService.getById(userId);
        if (StrUtil.equals("1", sysUser.getStatus())) {
            throw new StatusException("账号已禁用");
        }
        if (!StrUtil.equals("user", sysUser.getRole())) {
            throw new StatusException("账号异常");
        }
        return userId;
    }


    public SysUser getSysUser() {
        Long userId = getLoginUser().getUserId();
        SysUser sysUser = userService.getById(userId);
        if (StrUtil.equals("1", sysUser.getStatus())) {
            throw new StatusException("账号已禁用");
        }
        if (!StrUtil.equals("user", sysUser.getRole())) {
            throw new StatusException("账号异常");
        }
        if (StrUtil.isBlank(sysUser.getRandomSecretKey())) {
            throw new BusiException("请前往个人中心绑定谷歌");
        }
        return sysUser;
    }


}
